# Load default data into the database:

def init():
    
    # Create the role admin
    role_id=0
    
    messages=[]
    
    if db(db.auth_group.role == 'admin').count() == 0:
        role_id=auth.add_group('admin', 'super users who can admin Pootle')
        
        messages.append(LI(T('Role admin created')))
        
    else:
        role_admin=db(db.auth_group.role == 'admin').select().first()
        role_id=role_admin.id
                                
    # Check whether a super user exist:
    if db(db.auth_membership.group_id == role_id).count()==0:
        # Add user and assigns him/her the role admin
        
        password=db.auth_user.password.validate('admin')[0]

        
        user_id = db.auth_user.insert(first_name='admin',
                                     last_name='admin',
                                     email='admin@pootle2py.org',
                                     password=password)
        messages.append(LI(T('User admin@pootle2py.org created (password admin)')))        
        auth.add_membership(role_id, user_id)
        
        messages.append(LI(T('Assigned role admin to the user admin@pootle2py.org')))
    
    # Add languages:
    
    languages=_languages()
    
    for l in languages:
        if db(db.language.code==l).count()==0:
            db.language.insert(code=l, description=languages[l])
            messages.append(LI(T('Create language %s(%s)' %(languages[l], l))))
            
        
    if len(messages)==0: messages.append(T('Nothing To Do.'))
                        
    return HTML(BODY(H4(T('Init application')), UL(messages)))


def _languages():
    
    languages={}
    
    languages['en']='English'
    languages['de']='German'
    languages['es']='Spanish'
    languages['fr']='French'
    languages['it']='Italian'
    languages['ln']='Dutch'
    languages['ar']='Arabic'
    languages['pt']='Portuguese'
    languages['el']='Greek'
    
    return languages
    
    
    
    
    
    
    
    
